#include "dpLibrary\dpWindows.h"
#include "dpLibrary\graphics.h"
#include "dpLibrary\dpShared.h"

#include "winlib.h"

#include "AFObjectFactory.h"
#include "AFGraphics.h" 


// Make this function work with any two sprites
//void isSpriteAbove(Sprite* character, Sprite* spriteList)
//{
//  // Iterates through the linked list to find
//  // an object with the same x-values as the player.
//  Sprite* currentSprite = spriteList;
//  Sprite* messageSprite = malloc(sizeof(Sprite));
//
//  messageSprite->color = 0xFF0000;
//  messageSprite->height = 20;
//  messageSprite->width = 20;
//  messageSprite->screen.X = 200;
//  messageSprite->screen.Y = 200;
//
//  while (currentSprite)
//  {
//    if ((currentSprite->world.X) < character->world.X && 
//      character->world.X < (currentSprite->world.X + currentSprite->width))
//    {     
//      DrawSprite(messageSprite);     
//    }
//
//    currentSprite = currentSprite->next;
//  } 
//}

//void MovementControl(Sprite *controlledSprite, Sprite *spriteList, float velocity, float acceleration)
//{
//  int time = TimeGetTime();
//  int timePressed;
//  controlledSprite->isControlled = 1; /* isControlled determines which sprite the camera should follow */
//	
//  if(keyboard.wKey)
//    {
//
//      MoveCamera(spriteListHead, 0, -2);
//    }
//    if(keyboard.aKey)
//    {
//      MoveCamera(spriteListHead, -2, 0);
//    }
//    if(keyboard.sKey)
//    {
//      MoveCamera(spriteListHead, 0, 2);
//    }
//    if(keyboard.dKey)
//    {
//      MoveCamera(spriteListHead, 2, 0);
//    }
//	switch (keyPress)
//	{
//		case 'W':
//          timePressed = TimeGetTime() - time;
//			velocity = velocity + acceleration * timePressed; 
//			controlledSprite->position.X = controlledSprite->position.Y + velocity * timePressed;
//          time = TimeGetTime();
//			break;
//		case 'S':
//			velocity = velocity - acceleration * time 
//			controlledSprite->position.X = controlledSprite->position.Y + velocity * time;
//			break;
//		case 'A':
//			velocity = velocity - acceleration * time 
//			controlledSprite->position.X = controlledSprite->position.X + velocity * time;
//			break;
//		case 'D':
//			velocity = velocity + acceleration * time 
//			controlledSprite->position.X = controlledSprite->position.X + velocity * time;
//			break;
//		default:
//			MoveCamera(spriteListHead, 0, 0);
//			break;
//	}
//	controlledSprite->isControlled = 0;
//}


int CollisionDetection(Sprite *controlledSprite, Sprite *spriteList)
{
  /* Square-Square Collision */
  /* Coordinates of Left, Right, Top, Bottom borders of the two squares */
  int currentL;
  int currentR;
  int currentT;
  int currentB;
  int controlledL;
  int controlledR;
  int controlledT;
  int controlledB;
  int i; /* used for return value */

  Sprite *currentSprite = spriteList;

  while (currentSprite)
  {
    currentL = (currentSprite->world.X);
    currentR = (currentSprite->world.X + currentSprite->width);
    currentT = (currentSprite->world.Y);
    currentB = (currentSprite->world.Y + currentSprite->height);

    controlledL = (controlledSprite->world.X);
    controlledR = (controlledSprite->world.X + controlledSprite->width);
    controlledT = (controlledSprite->world.Y);
    controlledB = (controlledSprite->world.Y + controlledSprite->height);

    if ((currentR > controlledL) &&  (currentL < controlledR) && (currentT < controlledB)
      && (currentB > controlledT))
    {     
      controlledSprite->color = RED;    /* sets sprite to red if collision detected */
      currentSprite = currentSprite->next;
      return 1;
    }
    else
    {
      controlledSprite->color = GREEN; /* sets sprite back to green if no collision */
      currentSprite = currentSprite->next;
    }
  }

  return 0;
}

// Draws a line upward from the player sprite, stops drawing when it hits
// an enemy object.
//void MindControlRay(Sprite* character, Sprite *spriteList)
//{
//  /* Coordinates of Left, Right, Top, Bottom borders of the two squares */
//  int currentL;
//  int currentR;
//  int currentT;
//  int currentB;
//
//  currentL = (currentSprite->position.X);
//  currentR = (currentSprite->position.X + currentSprite->width);
//  currentT = (currentSprite->position.Y);
//  currentB = (currentSprite->position.Y + currentSprite->height);
//
//  int x = 0;
//  int y = 0;
//
//  Sprite *currentSprite = spriteList;
//
//
//  for(y = (dpScreenHeight / 2) - (character->height / 2) - 1; y > 0; --y)
//  {
//    if(y  )
//    {
//      *(screenBuffer + ((dpScreenWidth / 2) + (character->width / 2))
//        + y * dpScreenWidth) = 0x0000FF;       
//    }    
//    else
//    {
//      break;
//    }
//  }
//}